package com.kiln.juxingqs.ui

import androidx.viewpager.widget.ViewPager
import client.global.commlib.toast.toast
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.WdjpFragmentPageAdapter
import com.kiln.juxingqs.databinding.ActivityWdjpBinding
import com.kiln.juxingqs.ui.vm.WdjpViewModel
import com.kiln.juxingqs.BaseMiddleActivity
//我的竞拍

class WdjpActivity :
    BaseMiddleActivity<WdjpViewModel, ActivityWdjpBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_wdjp
    }

    val adapter: WdjpFragmentPageAdapter by lazy {
        WdjpFragmentPageAdapter(supportFragmentManager)
    }

    override fun initViewModel(): WdjpViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        binding.viewPager.adapter = adapter

        var goods_id = intent.getStringExtra("goods_id")
        goods_id?.let {
            viewModel.goods_id.value = it
        }

        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                updateTab(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
    }

    fun clickTab(index: Int) {
        binding.viewPager.setCurrentItem(index, false)
    }

    fun updateTab(index: Int) {

        when (index) {
            0 -> {
                binding.oneTab.isSelected = true
                binding.twoTab.isSelected = false
                binding.threeTab.isSelected = false
            }
            1 -> {
                binding.oneTab.isSelected = false
                binding.twoTab.isSelected = true
                binding.threeTab.isSelected = false
            }
            2 -> {
                binding.oneTab.isSelected = false
                binding.twoTab.isSelected = false
                binding.threeTab.isSelected = true
            }
        }
    }

    override fun initData() {
        updateTab(0)
    }

    override fun observerUI() {
        viewModel.cancleRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("已取消")
                    viewModel.pageOne.value = 0
                    viewModel.holdOne()
                    viewModel.pageTwo.value = 0
                    viewModel.holdTwo()
                    viewModel.pageThree.value = 0
                    viewModel.holdThree()
                }
            }
        })
    }

}